<?php

namespace XWAM\Controller;

/**
 * 用户控制器
 * <br />中转站
 *
 * @since 1.0 <2014-6-13> sutroon Added.
 */
class User extends AppbaseController {

    // 个人资料跳转
    function profile() {
        $type_name = $this->loginData['type_name'];
        $urls = array(
            'SYSTEMADMIN' => U('Admin/profile'),
            'SITEADMIN' => U('Admin/profile'),
            'ADMIN' => U('Admin/profile'),
            'SERVICES' => U('Services/profile'),
            'CUSTOMER' => U('Customer/profile'),
            'SEAT' => U('Seat/profile'),
            'EMPLOYEE' => U('Employee/profile'),
        );
        if (array_key_exists($type_name, $urls)) {
            redirect($urls[$type_name]);
        } else {
            exit('不支持的用户类型[' . $type_name . ']!');
        }
    }

    function setting() {
        $id = $this->loginData['id'];
        if (!$id) {
            exit('未登录!');
        }
        $mUser = new \XWAM\Model\UserModel();
        $data = $mUser->field('id, page_size, lang, theme')->find($id);
        $this->assign('data', $data);
        $this->display();
    }

    public function settingSave() {
        $data = I('post.');
        $data['id'] = $this->loginData['id'];
        $mUser = new \XWAM\Model\UserModel();
        $result = $mUser->save($data);
        $this->dialogClose($result, array('error' => $mUser->getError()));
    }

    /**
     * 编辑密码
     * @since 1.0 <2015-11-11> SoChishun Added.
     */
    function password_edit($id = 0) {
        if (!$id) {
            $id = $this->loginData['id'];
        }
        $this->assign('uid', $id);
        $this->display();
    }

    /**
     * 保存密码
     * @since 1.0 <2016-3-11> SoChishun Added.
     */
    function passwordEditSave() {
        $mUser = new \XWAM\Model\UserModel();
        $result = $mUser->savePassword();
        $this->dialogClose($result['status'], array('success' => '保存成功', 'error' => $result['info']));
    }

    // 重置密码
    public function resetPassword($id = 0) {
        if (!$id) {
            $this->result(false, '参数有误!');
        }
        $mUser = new \XWAM\Model\UserModel();
        $result = $mUser->where(array('id' => $id))->setField('password', '123456');
        $this->ajaxMsg($result, '密码重置{%}!');
    }

    // 更改用户状态
    function changeStatus($id, $status) {
        $mUser = new \XWAM\Model\UserModel();
        $this->ajaxReturn($mUser->changeStatus($id, $status));
    }

    // 更改用户在线状态
    function changeOnlineStatus($id, $status) {
        $mUser = new \XWAM\Model\UserModel();
        $this->ajaxReturn($mUser->changeOnlineStatus($id, $status));
    }

    // 2016-1-20
    function role($id) {
        $mUser = new \XWAM\Model\UserModel();
        $list = $mUser->where(array('id' => array('in', $id)))->field('id, user_name, role_id')->select();
        $this->assign('list', $list);
        $this->display();
    }

    function roles($id) {
        $mUser = new \XWAM\Model\UserModel();
        $role_id = $mUser->field('id,role_id')->find($id);
        $m_role = new \XWAM\Model\RoleModel();
        $list = $m_role->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 保存角色
     * @since 1.0 <2015-8-4> SoChishun Added.
     */
    public function roleSave() {
        $aid = I('id');
        $aRoleId = I('role_id');
        $mUser = new \XWAM\Model\UserModel();
        $result = $mUser->saveRole($aid, $aRoleId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

    // 2016-1-20
    function department($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mUser = new \XWAM\Model\UserModel();
        $list = $mUser->where(array('id' => array('in', $id)))->field('id, user_name, department_id')->select();
        $this->assign('list', $list);
        $this->display();
    }

    function departments($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mUser = new \XWAM\Model\UserModel();
        $department_id = $mUser->where(array('id' => $id))->getField('department_id');
        $m_department = new \XWAM\Model\DepartmentModel();
        $this->assign('tree', $m_department->selectTree());
        $this->assign('department_id', $department_id);
        $this->assign('id', $id);
        $this->display();
    }

    /**
     * 保存部门
     * @since 1.0 <2015-8-4> SoChishun Added.
     */
    public function departmentSave() {
        $aid = I('id');
        $aDepartmentId = I('department_id');
        $mUser = new \XWAM\Model\UserModel();
        $result = $mUser->saveDepartment($aid, $aDepartmentId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

}
